function varargout = iCalibrateHorz(varargin)
% ICALIBRATEHORZ M-file for iCalibrateHorz.fig
%      ICALIBRATEHORZ, by itself, creates a new ICALIBRATEHORZ or raises the existing
%      singleton*.
%
%      H = ICALIBRATEHORZ returns the handle to a new ICALIBRATEHORZ or the handle to
%      the existing singleton*.
%
%      ICALIBRATEHORZ('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ICALIBRATEHORZ.M with the given input arguments.
%
%      ICALIBRATEHORZ('Property','Value',...) creates a new ICALIBRATEHORZ or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before iCalibrateHorz_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to iCalibrateHorz_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% editfrontinput the above text to modify the response to help iCalibrateHorz

% Last Modified by GUIDE v2.5 25-Aug-2013 18:26:49

% Begin initialization code - DO NOT EDITFRONTINPUT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @iCalibrateHorz_OpeningFcn, ...
                   'gui_OutputFcn',  @iCalibrateHorz_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDITFRONTINPUT








function OnOpen(handles)

global angle;
global nfile;
global ncurrentframe;

global inputfront;
global inputside;
global outputfront;
global outputside;

global inputfrontfull;
global inputsidefull;
global outputfrontfull;
global outputsidefull;
global totalframes;

global mmreadfront;
global mmreadside;

global bmmreadfront;
global bmmreadside;

global outputpathname;

global totalframes;
global img;
global imgx;
global imgy;

global fflx; 
global ffly; 
global ffrx; 
global ffry; 
global fblx; 
global fbly; 
global fbrx; 
global fbry;

global sflx; 
global sfly; 
global sfrx; 
global sfry; 
global sblx; 
global sbly; 
global sbrx; 
global sbry;

global bInit;

global v;


global ncurrentframefront;
global ncurrentframeside;

global totalframesfront;
global imgfront;
global imgxfront;
global imgyfront;

global totalframesside;
global imgside;
global imgxside;
global imgyside;


fflx = 0;
ffly = 0;
ffrx = 0;
ffry = 0;
fblx = 0;
fbly = 0;
fbrx = 0;
fbry = 0;

sflx = 0;
sfly = 0;
sfrx = 0;
sfry = 0;
sblx = 0;
sbly = 0;
sbrx = 0;
sbry = 0;


nfile = 1;
angle = 1;
ncurrentframe = 0;
totalframes = 0;

bmmreadfront = 0;
bmmreadside = 0;



if angle == 1
    set(handles.radiobuttonfront, 'Value', 1);
    set(handles.radiobuttonside, 'Value', 0);
else
    set(handles.radiobuttonfront, 'Value', 0);
    set(handles.radiobuttonside, 'Value', 1);
end


%{
[totalframes, FileName, img] = loadfile(nfile, angle, ncurrentframe);
%}

global bFrontView;
global bSideView;

bFrontView = 1;
bSideView = 0;

savefile = 'videoprocessdata.mat';
load(savefile);

infile = '';

if ~isempty(inputfilefull)
    
    scheck = dir(inputfilefull); 
    
    if ~isempty(scheck)
        infile = inputfilefull;
    end
       
elseif~isempty(inputfile)
    scheck = dir(inputfile);    
    
    if ~isempty(scheck)
        infile = inputfile;
        inputfilefull = inputfile;
    end
end




if bSideView == 0
    set(handles.radiobuttonfront, 'Value', 1);
    set(handles.radiobuttonside, 'Value', 0);
else
    set(handles.radiobuttonfront, 'Value', 0);
    set(handles.radiobuttonside, 'Value', 1);
end

outputpathname = '';
inputfront  = '';
outputfront = '';

inputside   = '';
outputside  = '';


inputfrontfull  = '';
outputfrontfull = '';

inputsidefull   = '';
outputsidefull  = '';



bffront = strfind(upper(inputfile), 'FRONT');

if isempty(bffront)
    bfside = strfind(upper(inputfile), 'SIDE');
    
    if ~isempty(bfside)
        bFrontView = 0;
    else
        bFrontView = -1;
    end
else
    bFrontView = 1;
end



if ~isempty(calib2d3dfilefronthorz) && ~isempty(calib2d3dfilefronthorzfull)    
    outputfront = calib2d3dfilefronthorz;
    outputfrontfull = calib2d3dfilefronthorzfull;
end

if ~isempty(calib2d3dfilesidehorz) && ~isempty(calib2d3dfilesidehorzfull)
    outputside = calib2d3dfilesidehorz;
    outputsidefull = calib2d3dfilesidehorzfull;
end

if ~isempty(infile)
        
    if bFrontView == 0
        set(handles.radiobuttonfront, 'Value', 0);
        set(handles.radiobuttonside,  'Value', 1);                              
        
        inputside = inputfile;
        inputsidefull = inputfilefull;
        
        a = strfind(inputside, '.');
        
        if ~isempty(a) && isempty(outputside)
            outputside = strcat(inputside(1:a-1), '_calibrate_horz.mat');
            outputsidefull = strcat(pathname, outputside);                                 
        end

        
    elseif bFrontView == 1   
        set(handles.radiobuttonfront, 'Value', 1);
        set(handles.radiobuttonside,  'Value', 0);     
        
        inputfront = inputfile;
        inputfrontfull = inputfilefull;
        
        a = strfind(inputfront, '.');
        
        if ~isempty(a) && isempty(outputfront)
            outputfront = strcat(inputfront(1:a-1), '_calibrate_horz.mat');
            outputfrontfull = strcat(pathname, outputfront);                                 
        end
        
        
    end
    
end


if isempty(inputside) & ~isempty(inputfront)
    a = strfind(upper(inputfront), 'FRONT');
    slen = length(inputfront);
    
    temp2 = a+5;
        
    s = inputfront(temp2:slen);
    inputside = strcat('Side', s);
    inputsidefull = strcat(pathname, inputside);
    
    s = inputfront(temp2:slen-4);    
    s = strcat(s, '_calibrate_horz.mat');
    outputside = strcat('Side', s);
    outputsidefull = strcat(pathname, outputside); 
  
end

if isempty(inputfront) & ~isempty(inputside)
    a = strfind(upper(inputside), 'SIDE');
    slen = length(inputside);
    
    temp2 = a+4;
    
    s = inputside(temp2:slen);
    inputfront = strcat('Front', s);
    inputfrontfull = strcat(pathname, inputside);
    
    s = inputside(temp2:slen-4);    
    s = strcat(s, '_calibrate_horz.mat');
    outputfront = strcat('Front', s);
    outputfrontfull = strcat(pathname, outputside); 
    
end

if ~isempty(inputfrontfull) 
    
    scheck = dir(inputfrontfull);
    if isempty(scheck)
        inputfront = '';
        inputfrontfull = '';
        outputfront = '';
        outputfrontfull = '';
    end
end

if ~isempty(inputsidefull) 
    
    scheck = dir(inputsidefull);
    if isempty(scheck)
        inputside = '';
        inputsidefull = '';
        outputside = '';
        outputsidefull = '';
    end
end

if bFrontView == 0 || bFrontView == 1
    
    if bFrontView == 1
        set(handles.radiobuttonfront, 'Value', 1);
        set(handles.radiobuttonside, 'Value', 0);
    else
        set(handles.radiobuttonfront, 'Value', 0);
        set(handles.radiobuttonside, 'Value', 1);
    end
    
    InitVideo(handles);
end




if bFrontView == 1 & ~isempty(inputfrontfull)    
    
    mmreadfront = mmreader(inputfrontfull);
    bmmreadfront = 1;
   
    totalframesfront = mmreadfront.NumberOfFrames;  
    ncurrentframefront = 1;       
    
    bInit = 1;
end

if ~isempty(inputsidefull)
    
    mmreadside = mmreader(inputsidefull);
    bmmreadside = 1;
    
    totalframesside = mmreadside.NumberOfFrames;  
    ncurrentframeside = 1;
    
    bInit = 1;
end

if bFrontView == 1 & ~isempty(inputfrontfull)
    set(handles.radiobuttonfront, 'Value', 1);
    set(handles.radiobuttonside, 'Value', 0);
    
    set(handles.editframe, 'String', num2str(ncurrentframefront));
    set(handles.texttotalframes, 'String', num2str(totalframesfront));
    
    v = mmreadfront;
    
    imgfront = read(mmreadfront,ncurrentframefront);                 
    sz = size(imgfront);
    imgxfront = sz(2);
    imgyfront = sz(1);
    
    imshow(imgfront);     
elseif bFrontView == 0 & ~isempty(inputsidefull)  
    
    set(handles.radiobuttonfront, 'Value', 0);
    set(handles.radiobuttonside, 'Value', 1);
    
    set(handles.editframe, 'String', num2str(ncurrentframeside));
    set(handles.texttotalframes, 'String', num2str(totalframesside));
    
    v = mmreadside;            
    
    imgside = read(mmreadside,ncurrentframeside);                 
    sz = size(imgside);
    imgxside = sz(2);
    imgyside = sz(1);
        
    imshow(imgside);     
end

if ~isempty(outputfrontfull)
    LoadDataFile(outputfrontfull, handles);
end

if ~isempty(outputsidefull)
    LoadDataFile(outputsidefull, handles);
end

     
set(handles.editfrontinput, 'String', inputfront);
set(handles.editfrontdata, 'String', outputfront);
set(handles.editsideinput, 'String', inputside);
set(handles.editsidedata, 'String', outputside);


UpdateTextBoxes(handles);






function InitVideo(handles)

global inputfrontfull;
global inputsidefull;
global v;

global ncurrentframefront;
global ncurrentframeside;

global totalframesfront;
global imgfront;
global imgxfront;
global imgyfront;

global totalframesside;
global imgside;
global imgxside;
global imgyside;

global flxfront; 
global flyfront; 
global frxfront; 
global fryfront; 
global blxfront; 
global blyfront; 
global brxfront; 
global bryfront;

global flxside; 
global flyside; 
global frxside; 
global fryside; 
global blxside; 
global blyside; 
global brxside; 
global bryside;


global mmreadfront;
global mmreadside;

global bmmreadfront;
global bmmreadside;

global bInit;

bFront = get(handles.radiobuttonfront, 'Value');


bInit = 0;

if bFront == 1 & ~isempty(inputfrontfull)    
    
    mmreadfront = mmreader(inputfrontfull);
    bmmreadfront = 1;
    
    v = mmreadfront;
     
    totalframesfront = mmreadfront.NumberOfFrames;  
    ncurrentframefront = 1;
    
    
    imgfront = read(mmreadfront,ncurrentframefront);                 
    sz = size(imgfront);
    imgxfront = sz(2);
    imgyfront = sz(1);
    
    imshow(imgfront);
       
    set(handles.radiobuttonfront, 'Value', 1);
    set(handles.radiobuttonside, 'Value', 0);
    
    set(handles.editframe, 'String', num2str(ncurrentframefront));
    set(handles.texttotalframes, 'String', num2str(totalframesfront));
    
    bInit = 1;
elseif ~isempty(inputsidefull)
    
    mmreadside = mmreader(inputsidefull);
    bmmreadside = 1;
    
    v = mmreadside;
    
    totalframesside = mmreadside.NumberOfFrames;  
    ncurrentframeside = 1;
    
    imgside = read(mmreadside,ncurrentframeside);                 
    sz = size(imgside);
    imgxside = sz(2);
    imgyside = sz(1);
        
    imshow(imgside);     
        
    set(handles.radiobuttonfront, 'Value', 0);
    set(handles.radiobuttonside, 'Value', 1);
    
    set(handles.editframe, 'String', num2str(ncurrentframeside));
    set(handles.texttotalframes, 'String', num2str(totalframesside));
    
    bInit = 1;
end




    


function UpdateTextBoxes(handles)

global fflx; 
global ffly; 
global ffrx; 
global ffry; 
global fblx; 
global fbly; 
global fbrx; 
global fbry; 

global sflx; 
global sfly; 
global sfrx; 
global sfry; 
global sblx; 
global sbly; 
global sbrx; 
global sbry; 


global ffdist;
global fbdist;

global sfdist;
global sbdist;

global foffscreenleftdist;
global soffscreenleftdist;

global ffrontbackdist;
global sfrontbackdist;

global pffdist;
global pfbdist;
global psfdist;
global psbdist;

global ffpmm;
global fbpmm;
global sfpmm;
global sbpmm;

global af;
global bf;
global kf;
global tf;
global df;

global as;
global bs;
global ks;
global ts;
global ds;

global angle;


fdist = str2double(get(handles.editfdist, 'String'));   %front - distance in mm
bdist = str2double(get(handles.editbdist, 'String'));   %back  - distance in mm

offscreenleftdist = str2double(get(handles.editleftdist, 'String'));   %off screen distance - mm
frontbackdist = str2double(get(handles.editfrontbackdist, 'String'));   %front to back distance - mm

fpmm = 0;
bpmm = 0;

af = 0;
bf = 0;
kf = 0;
tf = 0;

as = 0;
bs = 0;
ks = 0;
ts = 0;
ds = 0;


flx = str2double(get(handles.editflx, 'String'));
fly = str2double(get(handles.editfly, 'String'));
frx = str2double(get(handles.editfrx, 'String'));
fry = str2double(get(handles.editfry, 'String'));
blx = str2double(get(handles.editblx, 'String'));
bly = str2double(get(handles.editbly, 'String'));
brx = str2double(get(handles.editbrx, 'String'));
bry = str2double(get(handles.editbry, 'String'));
    
pfdist = abs(frx-flx);
pbdist = abs(brx-blx);

if fdist > 0
    fpmm = pfdist/fdist;
end

if bdist > 0
    bpmm = pbdist/bdist;
end

set(handles.textpfdist, 'String', num2str(pfdist));
set(handles.textpbdist, 'String', num2str(pbdist));

set(handles.textfpmm, 'String', num2str(fpmm));
set(handles.textbpmm, 'String', num2str(bpmm));

set(handles.textbackleft, 'String', num2str(blx));
   

if angle == 1       
    
    fflx = flx;
    ffly = fly;
    ffrx = frx;
    ffry = fry;
    fblx = blx;
    fbly = bly;
    fbrx = brx;
    fbry = bry;
    
    ffdist = fdist;
    fbdist = bdist;
    
    ffpmm = fpmm;
    fbpmm = bpmm;
    
    pffdist = pfdist;
    pfbdist = pbdist;
    
    foffscreenleftdist = offscreenleftdist;
    ffrontbackdist = frontbackdist;

else
    
    sflx = flx;
    sfly = fly;
    sfrx = frx;
    sfry = fry;
    sblx = blx;
    sbly = bly;
    sbrx = brx;
    sbry = bry;
    
    
    sfdist = fdist;
    sbdist = bdist;
    
    sfpmm = fpmm;
    sbpmm = bpmm;
    
    psfdist = pfdist;
    psbdist = pbdist;
    
    soffscreenleftdist = offscreenleftdist;
    sfrontbackdist = frontbackdist;

end



af = -ffpmm*foffscreenleftdist;              

if ffpmm > 0 & fbpmm > 0 
    if ffrontbackdist > 0
        kf = -(ffpmm-fbpmm)/ffrontbackdist;
        
        tf = -af+fblx;                
        bf = tf/ffrontbackdist;        
    end
end
    
df = ffpmm; 


    
     
as = sblx;    
        
if sfpmm > 0 & sbpmm > 0 
	if sfrontbackdist > 0
        ks = (sfpmm-sbpmm)/sfrontbackdist;                   
            
        ts = as+(soffscreenleftdist*sfpmm);    
        bs = -ts/sfrontbackdist;
    end
end
    
ds = sbpmm;
    

    
    
if angle == 1       
    set(handles.textndf, 'String', 'df');
    set(handles.textnds, 'String', '');
    set(handles.textnas, 'String', '');
    set(handles.textnaf, 'String', 'af');
    set(handles.textnbfbs, 'String', 'bf');
    set(handles.textnkfks, 'String', 'kf');              
    
    set(handles.textaf, 'String', num2str(af));
    set(handles.textbfbs, 'String', num2str(bf));
    set(handles.textkfks, 'String', num2str(kf));
else
    set(handles.textndf, 'String', '');
    set(handles.textnds, 'String', 'ds');
    set(handles.textnas, 'String', 'as');
    set(handles.textnaf, 'String', '');
    set(handles.textnbfbs, 'String', 'bs');
    set(handles.textnkfks, 'String', 'ks');
    
    set(handles.textbfbs, 'String', num2str(bs));
    set(handles.textkfks, 'String', num2str(ks));
end


    
set(handles.textoutput, 'String', '');




function MouseButtonDown(handles)

global imgx;
global imgy;


point = get(gca,'CurrentPoint');

x = round(point(1,1));
y = round(point(1,2));


fl = get(handles.radiobuttonfl, 'Value');
fr = get(handles.radiobuttonfr, 'Value');
bl = get(handles.radiobuttonbl, 'Value');
br = get(handles.radiobuttonbr, 'Value');


if x > 0 & y > 0 & x <= imgx & y <= imgy
    
    if fl == 1
        flx = x;
        fly = y;
        
        set(handles.editflx, 'String', num2str(flx));
        set(handles.editfly, 'String', num2str(fly));
    elseif fr == 1
        frx = x;
        fry = y;
                
        set(handles.editfrx, 'String', num2str(frx));
        set(handles.editfry, 'String', num2str(fry));
    elseif bl == 1
        blx = x;
        bly = y;
        
        set(handles.editblx, 'String', num2str(blx));
        set(handles.editbly, 'String', num2str(bly));
    elseif br == 1
        brx = x;
        bry = y;    
        
        set(handles.editbrx, 'String', num2str(brx));
        set(handles.editbry, 'String', num2str(bry));    
    end                   
end

UpdateTextBoxes(handles)
    





% --- Executes just before iCalibrateHorz is made visible.
function iCalibrateHorz_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to iCalibrateHorz (see VARARGIN)

% Choose default command line output for iCalibrateHorz
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes iCalibrateHorz wait for user response (see UIRESUME)
% uiwait(handles.figure1);


OnOpen(handles)



% --- Outputs from this function are returned to the command line.
function varargout = iCalibrateHorz_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


MouseButtonDown(handles)



function editfdist_Callback(hObject, eventdata, handles)
% hObject    handle to editfdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfdist as text
%        str2double(get(hObject,'String')) returns contents of editfdist as a double


% --- Executes during object creation, after setting all properties.
function editfdist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editbdist_Callback(hObject, eventdata, handles)
% hObject    handle to editbdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editbdist as text
%        str2double(get(hObject,'String')) returns contents of editbdist as a double


% --- Executes during object creation, after setting all properties.
function editbdist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editbdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editleftdist_Callback(hObject, eventdata, handles)
% hObject    handle to editleftdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editleftdist as text
%        str2double(get(hObject,'String')) returns contents of editleftdist as a double


% --- Executes during object creation, after setting all properties.
function editleftdist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editleftdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on editfdist and none of its controls.
function editfdist_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to editfdist (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

UpdateTextBoxes(handles)



% --- Executes on key press with focus on editbdist and none of its controls.
function editbdist_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to editbdist (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

UpdateTextBoxes(handles)


% --- Executes on key press with focus on editleftdist and none of its controls.
function editleftdist_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to editleftdist (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



function editflx_Callback(hObject, eventdata, handles)
% hObject    handle to editflx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editflx as text
%        str2double(get(hObject,'String')) returns contents of editflx as a double


% --- Executes during object creation, after setting all properties.
function editflx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editflx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfly_Callback(hObject, eventdata, handles)
% hObject    handle to editfly (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfly as text
%        str2double(get(hObject,'String')) returns contents of editfly as a double


% --- Executes during object creation, after setting all properties.
function editfly_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfly (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfrx_Callback(hObject, eventdata, handles)
% hObject    handle to editfrx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfrx as text
%        str2double(get(hObject,'String')) returns contents of editfrx as a double


% --- Executes during object creation, after setting all properties.
function editfrx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfrx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfry_Callback(hObject, eventdata, handles)
% hObject    handle to editfry (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfry as text
%        str2double(get(hObject,'String')) returns contents of editfry as a double


% --- Executes during object creation, after setting all properties.
function editfry_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfry (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editblx_Callback(hObject, eventdata, handles)
% hObject    handle to editblx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editblx as text
%        str2double(get(hObject,'String')) returns contents of editblx as a double


% --- Executes during object creation, after setting all properties.
function editblx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editblx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editbly_Callback(hObject, eventdata, handles)
% hObject    handle to editbly (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editbly as text
%        str2double(get(hObject,'String')) returns contents of editbly as a double


% --- Executes during object creation, after setting all properties.
function editbly_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editbly (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editbrx_Callback(hObject, eventdata, handles)
% hObject    handle to editbrx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editbrx as text
%        str2double(get(hObject,'String')) returns contents of editbrx as a double


% --- Executes during object creation, after setting all properties.
function editbrx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editbrx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editbry_Callback(hObject, eventdata, handles)
% hObject    handle to editbry (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editbry as text
%        str2double(get(hObject,'String')) returns contents of editbry as a double


% --- Executes during object creation, after setting all properties.
function editbry_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editbry (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfalx_Callback(hObject, eventdata, handles)
% hObject    handle to editfalx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfalx as text
%        str2double(get(hObject,'String')) returns contents of editfalx as a double


% --- Executes during object creation, after setting all properties.
function editfalx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfalx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfaly_Callback(hObject, eventdata, handles)
% hObject    handle to editfaly (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfaly as text
%        str2double(get(hObject,'String')) returns contents of editfaly as a double


% --- Executes during object creation, after setting all properties.
function editfaly_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfaly (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfrontbackdist_Callback(hObject, eventdata, handles)
% hObject    handle to editfrontbackdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfrontbackdist as text
%        str2double(get(hObject,'String')) returns contents of editfrontbackdist as a double


% --- Executes during object creation, after setting all properties.
function editfrontbackdist_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfrontbackdist (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonsave.
function pushbuttonsave_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global fflx; 
global ffly; 
global ffrx; 
global ffry; 
global fblx; 
global fbly; 
global fbrx; 
global fbry; 


global sflx; 
global sfly; 
global sfrx; 
global sfry; 
global sblx; 
global sbly; 
global sbrx; 
global sbry; 

global ffdist;
global fbdist;

global sfdist;
global sbdist;

global foffscreenleftdist;
global soffscreenleftdist;

global ffrontbackdist;
global sfrontbackdist;

global pffdist;
global pfbdist;
global psfdist;
global psbdist;

global ffpmm;
global fbpmm;
global sfpmm;
global sbpmm;

global af;
global bf;
global df;
global kf;
global tf;

global as;
global bs;
global ds;
global ks;
global ts;

global outputpathname;
global outputfrontfull;
global outputsidefull;

UpdateTextBoxes(handles);


outputfront = get(handles.editfrontdata, 'String');
outputside = get(handles.editsidedata, 'String');

outputfrontfull = strcat(outputpathname, outputfront);
outputsidefull = strcat(outputpathname, outputside);



bSave = 0;

if ~isempty(outputfrontfull)            
    save(outputfrontfull, 'fflx', 'ffly', 'ffrx', 'ffry', 'fblx', 'fbly', 'fbrx', 'fbry',...
     'ffdist', 'fbdist', 'pffdist', 'pfbdist', 'ffpmm', 'fbpmm', 'af', 'bf', 'kf', 'tf', 'df',...
     'foffscreenleftdist', 'ffrontbackdist');
 
    bSave = 1;
end


if ~isempty(outputsidefull)           
    save(outputsidefull, 'sflx', 'sfly', 'sfrx', 'sfry', 'sblx', 'sbly', 'sbrx', 'sbry',...
     'sfdist', 'sbdist', 'psfdist', 'psbdist', 'sfpmm', 'sbpmm', 'as', 'bs', 'ks', 'ts', 'ds',...
     'soffscreenleftdist', 'sfrontbackdist');
 
    if bSave == 1
        bSave = 3;
    else
        bSave = 2;
    end
end

if bSave == 1
    set(handles.textoutput, 'String', 'Front data saved to file');
elseif bSave == 2
    set(handles.textoutput, 'String', 'Side data saved to file');
elseif bSave == 3
    set(handles.textoutput, 'String', 'Front and side data saved to file');
end
    


% --- Executes on button press in pushbuttonupdate.
function pushbuttonupdate_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonupdate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

UpdateTextBoxes(handles)


% --- Executes when selected object is changed in uipanelcameraview.
function uipanelcameraview_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanelcameraview 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)


global angle;
%global nfile;
global ncurrentframe;
global totalframes;

global img;
global imgx;
global imgy;

global inputfrontfull;
global inputsidefull;

%{
global flx; 
global fly; 
global frx; 
global fry; 
global blx; 
global bly; 
global brx; 
global bry;
%}

global mmreadfront;
global mmreadside;

global bmmreadfront;
global bmmreadside;

global v;

rf = get(handles.radiobuttonfront, 'Value');

UpdateTextBoxes(handles);



if isempty(inputfrontfull)
    
    s = get(handles.editfrontinput, 'String');
    
    scheck = dir(s);
    
    if ~isempty(scheck)
        inputfrontfull = s;        
        
        mmreadfront = mmreader(inputfrontfull);
        bmmreadfront = 1;        
    end
    
end



if isempty(inputsidefull)
    
    s = get(handles.editsideinput, 'String');
    
    scheck = dir(s);
   

    if ~isempty(scheck)
        inputsidefull = s;        
        
        mmreadside = mmreader(inputsidefull);
        bmmreadside = 1;        
    end
    
end




if rf == 1
    angle = 1;
    
    if ~isempty(inputfrontfull) & bmmreadfront == 0
        s = get(handles.editfrontinput, 'String');
    
        scheck = dir(s);
    
        if ~isempty(scheck)
            inputfrontfull = s;        
        
            mmreadfront = mmreader(inputfrontfull);
            bmmreadfront = 1;        
        end
    end
    
    if ~isempty(inputfrontfull) & bmmreadfront == 1
        
      ncurrentframe = 1;  
      %v = mmreader(inputfrontfull); 
      
      v = mmreadfront;
      totalframes = v.NumberOfFrames;  
    
      img = read(v,ncurrentframe);         
        
      imshow(img);
        
      sz = size(img);
      imgx = sz(2);
      imgy = sz(1);
      
      %{
      flx = 0;
      fly = 0;
      frx = 0;
      fry = 0;
      blx = 0;
      bly = 0;
      brx = 0;
      bry = 0;
      %}
      
      set(handles.editframe, 'String', num2str(ncurrentframe));
      set(handles.texttotalframes, 'String', num2str(totalframes));
    end
        
else
    angle = 2;        
        
    if ~isempty(inputsidefull) & bmmreadside == 0
        s = get(handles.editsideinput, 'String');
    
        scheck = dir(s);
   

        if ~isempty(scheck)
            inputsidefull = s;        
        
            mmreadside = mmreader(inputsidefull);
            bmmreadside = 1;        
        end
    end
    
    if ~isempty(inputsidefull) & bmmreadside == 1
        
        
      ncurrentframe = 1;  
      
      v = mmreadside;
      totalframes = v.NumberOfFrames;  
    
      img = read(v,ncurrentframe);         
        
      imshow(img);
        
      sz = size(img);
      imgx = sz(2);
      imgy = sz(1);
      
      set(handles.editframe, 'String', num2str(ncurrentframe));
      set(handles.texttotalframes, 'String', num2str(totalframes));
    end
end


UpdateFields(handles);
UpdateTextBoxes(handles);





function ButtonGotoFrame(handles)

global ncurrentframe;
global img;
global v;

scurrentframe = get(handles.editframe, 'String');
n = str2num(scurrentframe);

totalframes = v.NumberOfFrames;  

if n > 0 & n <= totalframes
    ncurrentframe = n;
    
    img = read(v,ncurrentframe);
    imshow(img)
end








%{
[totalframes, FileName, img] = loadfile(nfile, angle, ncurrentframe);

imshow(img)

sz = size(img);
imgx = sz(2);
imgy = sz(1);

UpdateTextBoxes(handles);
%}



function editfrontinput_Callback(hObject, eventdata, handles)
% hObject    handle to editfrontinput (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfrontinput as text
%        str2double(get(hObject,'String')) returns contents of editfrontinput as a double


% --- Executes during object creation, after setting all properties.
function editfrontinput_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfrontinput (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editsideinput_Callback(hObject, eventdata, handles)
% hObject    handle to editsideinput (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editsideinput as text
%        str2double(get(hObject,'String')) returns contents of editsideinput as a double


% --- Executes during object creation, after setting all properties.
function editsideinput_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editsideinput (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editfrontdata_Callback(hObject, eventdata, handles)
% hObject    handle to editfrontdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editfrontdata as text
%        str2double(get(hObject,'String')) returns contents of editfrontdata as a double


% --- Executes during object creation, after setting all properties.
function editfrontdata_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editfrontdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function editsidedata_Callback(hObject, eventdata, handles)
% hObject    handle to editsidedata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editsidedata as text
%        str2double(get(hObject,'String')) returns contents of editsidedata as a double


% --- Executes during object creation, after setting all properties.
function editsidedata_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editsidedata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: editfrontinput controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonfrontinput.
function pushbuttonfrontinput_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonfrontinput (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global inputfront;
global inputfrontfull;

global outputfront;
global outputfrontfull;

[filename, pathname] = uigetfile( ...
{'*.avi; *.mpg; *.mpeg','Video Files (*.avi, *.mpg, *.mpeg)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');


if filename ~= 0
    inputfront = filename;
    inputfrontfull = strcat(pathname, filename);
    
    flen = length(inputfront);
    
     if isempty(outputfrontfull) & flen > 0        
        outputfront = strcat(inputfront(1:flen-4), '_calibrate_horz.mat'); 
        outputfrontfull = strcat(pathname, outputfront);
        LoadDataFile(outputfrontfull, handles);
        
        UpdateFields(handles);
        
        set(handles.editfrontdata, 'String', outputfront);
    end
end

set(handles.editfrontinput, 'String', inputfront);


InitVideo(handles);
UpdateTextBoxes(handles);



% --- Executes on button press in pushbuttonsideinput.
function pushbuttonsideinput_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsideinput (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global inputside;
global inputsidefull;

global outputside;
global outputsidefull;

[filename, pathname] = uigetfile( ...
{'*.avi; *.mpg; *.mpeg','Video Files (*.avi, *.mpg, *.mpeg)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    inputside = filename;
    inputsidefull = strcat(pathname, filename);   
    
    flen = length(inputside);
    
    if isempty(outputsidefull) & flen > 0        
        outputside = strcat(inputside(1:flen-4), '_calibrate_horz.mat');
        outputsidefull = strcat(pathname, outputside);
        LoadDataFile(outputsidefull, handles);
        
        UpdateFields(handles);
        
        set(handles.editsidedata, 'String', outputside);
    end
end

set(handles.editsideinput, 'String', inputside);


InitVideo(handles);
UpdateTextBoxes(handles);


% --- Executes on button press in pushbuttonfrontdata.
function pushbuttonfrontdata_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonfrontdata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global outputfront;
global outputfrontfull;
global outputpathname;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    outputfront = filename;
    outputfrontfull = strcat(pathname, filename);   
end

set(handles.editfrontdata, 'String', outputfront);

LoadDataFile(outputfront, handles);
UpdateFields(handles);
UpdateTextBoxes(handles);


% --- Executes on button press in pushbuttonsidedata.
function pushbuttonsidedata_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonsidedata (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


global outputside;
global outputsidefull;
global outputpathname;

[filename, pathname] = uigetfile( ...
{'*.mat','Matlab Data Files (*.mat)';   
 '*.*',  'All Files (*.*)'}, ...
 'Pick a file');

if filename ~= 0
    outputside = filename;
    outputsidefull = strcat(pathname, filename);   
end

set(handles.editsidedata, 'String', outputside);

LoadDataFile(outputside, handles);
UpdateFields(handles);
UpdateTextBoxes(handles);




function LoadDataFile(f, handles)


global fflx; 
global ffly; 
global ffrx; 
global ffry; 
global fblx; 
global fbly; 
global fbrx; 
global fbry; 

global sflx; 
global sfly; 
global sfrx; 
global sfry; 
global sblx; 
global sbly; 
global sbrx; 
global sbry; 
                     
global ffdist;
global fbdist;

global sfdist;
global sbdist;

global foffscreenleftdist;
global soffscreenleftdist;

global ffrontbackdist;
global sfrontbackdist;

global pffdist;
global pfbdist;
global psfdist;
global psbdist;

global ffpmm;
global fbpmm;
global sfpmm;
global sbpmm;

global as;
global bs;
global ds;
global ks;
global ts;

global af;
global bf;
global df;
global kf;
global tf;


scheckfile = dir(f);    

if ~isempty(scheckfile)
    load(f);      
    
    UpdateFields(handles);
    UpdateTextBoxes(handles);
end   



function UpdateFields(handles)

global fflx; 
global ffly; 
global ffrx; 
global ffry; 
global fblx; 
global fbly; 
global fbrx; 
global fbry; 

global sflx; 
global sfly; 
global sfrx; 
global sfry; 
global sblx; 
global sbly; 
global sbrx; 
global sbry; 
                     
global ffdist;
global fbdist;

global sfdist;
global sbdist;

global foffscreenleftdist;
global soffscreenleftdist;

global ffrontbackdist;
global sfrontbackdist;

global pffdist;
global pfbdist;
global psfdist;
global psbdist;

global ffpmm;
global fbpmm;
global sfpmm;
global sbpmm;

global as;
global bs;
global ds;
global ks;
global ts;

global af;
global bf;
global df;
global kf;
global tf;

angle = get(handles.radiobuttonfront, 'Value');
    
if angle == 1
      set(handles.editflx, 'String', num2str(fflx));
      set(handles.editfrx, 'String', num2str(ffrx));
      set(handles.editfly, 'String', num2str(ffly));
      set(handles.editfry, 'String', num2str(ffry));
            
      set(handles.editblx, 'String', num2str(fblx));
      set(handles.editbrx, 'String', num2str(fbrx));
      set(handles.editbly, 'String', num2str(fbly));
      set(handles.editbry, 'String', num2str(fbry));
                  
      set(handles.editfdist, 'String', num2str(ffdist));   
      set(handles.editbdist, 'String', num2str(fbdist));   
     
      set(handles.editleftdist, 'String', num2str(foffscreenleftdist));
      set(handles.editfrontbackdist, 'String', num2str(ffrontbackdist));

else
      set(handles.editflx, 'String', num2str(sflx));
      set(handles.editfrx, 'String', num2str(sfrx));
      set(handles.editfly, 'String', num2str(sfly));
      set(handles.editfry, 'String', num2str(sfry));
            
      set(handles.editblx, 'String', num2str(sblx));
      set(handles.editbrx, 'String', num2str(sbrx));
      set(handles.editbly, 'String', num2str(sbly));
      set(handles.editbry, 'String', num2str(sbry));            
                        
      set(handles.editfdist, 'String', num2str(sfdist));   
      set(handles.editbdist, 'String', num2str(sbdist));   
     
      set(handles.editleftdist, 'String', num2str(soffscreenleftdist));
      set(handles.editfrontbackdist, 'String', num2str(sfrontbackdist));      
end
    
    

function editframe_Callback(hObject, eventdata, handles)
% hObject    handle to editframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editframe as text
%        str2double(get(hObject,'String')) returns contents of editframe as a double


% --- Executes during object creation, after setting all properties.
function editframe_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttongoframe.
function pushbuttongoframe_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttongoframe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ButtonGotoFrame(handles);
